#!/bin/bash

# Slackware build script for amd-app-sdk

# Copyright (c) 2011 Alan Alberghini <414N@slacky.it>
# All rights reserved.
#
#   Permission to use, copy, modify, and distribute this software for
#   any purpose with or without fee is hereby granted, provided that
#   the above copyright notice and this permission notice appear in all
#   copies.
#
#   THIS SOFTWARE IS PROVIDED AS IS'' AND ANY EXPRESSED OR IMPLIED
#   WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
#   MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
#   IN NO EVENT SHALL THE AUTHORS AND COPYRIGHT HOLDERS AND THEIR
#   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
#   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
#   USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
#   ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
#   OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
#   OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
#   SUCH DAMAGE.
# -----------------------------------------------------------------------------
#
# Build history:
#
# Initial release.
# 20121220 - Updated for version 2.6: incompatible packaging with 2.4- versions
# 20130530 - Updated for version 2.8 and made samples optional

cd $(dirname $0) ; CWD=$(pwd)

PRGNAM=amd-app-sdk
INT_NAME=AMD-APP-SDK
VERSION=${VERSION:-2.8}
BUILD=${BUILD:-1}
TAG=${TAG:-_SBo}
PKGTYPE=${PKGTYPE:-tgz}
SAMPLES=${SAMPLES:-no}

# (only x86 and x86_64 are supported)
TESTARCH=${ARCH:-`uname -m`}
case "$TESTARCH" in
  x86|i*86) ARCH=x86; BITNESS=32 ;;
  x86_64) ARCH=x86_64; BITNESS=64 ;;
  *) echo "$ARCH architecture not supported. Please specify a supported ARCH (x86 or x86_64)."
   	  exit 1 ;;
esac

# If the variable PRINT_PACKAGE_NAME is set, then this script will report what
# the name of the created package would be, and then exit. This information
# could be useful to other scripts.
if [ ! -z "${PRINT_PACKAGE_NAME}" ]; then
  echo "$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE"
  exit 0
fi

TMP=${TMP:-/tmp/SBo}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

ARCHIVE_NAME="AMD-APP-SDK-v$VERSION-lnx${BITNESS}"
INT_ARCHIVE_NAME=AMD-APP-SDK-v$VERSION-RC*-lnx${BITNESS}

set -e

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $INT_ARCHIVE_NAME
tar xvf $CWD/$ARCHIVE_NAME.tgz

# Keep out the samples from being extracted in the first place, if they're not
# desired.

if [ "$SAMPLES" == "no" ]
then
	EXCLUDES="--exclude $INT_ARCHIVE_NAME/samples"
fi

# If we're packaging a 64 bit package, let's exclude the 32 bit parts from the
# extraction (no multilib).

if [ ${ARCH} = x86_64 ]
then
	EXCLUDES+=" --exclude $INT_ARCHIVE_NAME/samples/opencl/bin/x86"
	EXCLUDES+=" --exclude $INT_ARCHIVE_NAME/lib/x86"
	EXCLUDES+=" --exclude $INT_ARCHIVE_NAME/bin/x86"
fi

tar xvf $INT_ARCHIVE_NAME.tgz $EXCLUDES
cd $INT_ARCHIVE_NAME

chown -R root:root .
find -L . \
 \( -perm 777 -o -perm 775 -o -perm 750 -o -perm 711 -o -perm 555 -o -perm 511 \) \
 -exec chmod 755 {} \; -o \
 \( -perm 666 -o -perm 664 -o -perm 600 -o -perm 444 -o -perm 440 -o -perm 400 \) \
 -exec chmod 644 {} \;

find $PKG | xargs file | grep -e "executable" -e "shared object" | grep ELF \
  | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null || true

# Create output directories and copy everything inside /opt

mkdir -p $PKG/opt/$PRGNAM $PKG/etc/profile.d $PKG/usr/doc/$PRGNAM-$VERSION

mv * $PKG/opt/$PRGNAM

# Install the icd registration tarball into $PKG root

( cd $PKG && tar xf $TMP/icd-registration.tgz )

# Copy the main profile scripts in their final location...

install -m0755 $CWD/amd-app-sdk.sh $PKG/etc/profile.d
install -m0755 $CWD/amd-app-sdk.csh $PKG/etc/profile.d

# ...and then the libs-related ones.

if [ ${ARCH} = x86 ]
then
	install -m0644 $CWD/amd-app-sdk-libs32.sh $PKG/etc/profile.d
	install -m0644 $CWD/amd-app-sdk-libs32.csh $PKG/etc/profile.d
else
        install -m0644 $CWD/amd-app-sdk-libs64.sh $PKG/etc/profile.d
        install -m0644 $CWD/amd-app-sdk-libs64.csh $PKG/etc/profile.d
fi

chown -R root:root $PKG/etc

mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION

# Link documentation and samples from /opt
ln -sf /opt/$PRGNAM/docs/opencl $PKG/usr/doc/$PRGNAM-$VERSION

if [  "$SAMPLES" != "no" ]
then
	ln -sf /opt/$PRGNAM/samples $PKG/usr/doc/$PRGNAM-$VERSION
fi

cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild
cat $CWD/README.SLACKWARE > $PKG/usr/doc/$PRGNAM-$VERSION/README.SLACKWARE

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc

cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE
